﻿namespace Nomo.CoreModule
{
    /// <summary>
    /// 数据库。
    /// </summary>
    public static class Database
    {
        private static readonly System.Collections.Generic.Dictionary<object, System.Collections.Generic.Dictionary<object, object>> Dictionary = new System.Collections.Generic.Dictionary<object, System.Collections.Generic.Dictionary<object, object>>();

        /// <summary>
        /// 设置值。
        /// </summary>
        /// <param name="domain">领域。</param>
        /// <param name="key">键。</param>
        /// <param name="value">值。</param>
        /// <exception cref="System.ArgumentNullException"><paramref name="domain"/> 或 <paramref name="key"/> 为 <see langword="null"/> .</exception>
        public static void Set(object domain, object key, object value)
        {
            if (domain == null)
            {
                throw new System.ArgumentNullException(nameof(domain));
            }

            if (key == null)
            {
                throw new System.ArgumentNullException(nameof(key));
            }

            if (Dictionary.TryGetValue(domain, out var domainDictionary))
            {
                domainDictionary[key] = value;
            }
            else
            {
                Dictionary[domain] = new System.Collections.Generic.Dictionary<object, object> {[key] = value};
            }
        }

        /// <summary>
        /// 获取值。
        /// </summary>
        /// <param name="domain">领域。</param>
        /// <param name="key">键。</param>
        /// <returns>获取到的值。</returns>
        /// <exception cref="System.ArgumentNullException"><paramref name="domain"/> 或 <paramref name="key"/> 为 <see langword="null"/> .</exception>
        /// <exception cref="System.Collections.Generic.KeyNotFoundException">未找到 <paramref name="domain"/> 或未找到 <paramref name="key"/> .</exception>
        public static object Get(object domain, object key)
        {
            if (domain == null)
            {
                throw new System.ArgumentNullException(nameof(domain));
            }

            if (key == null)
            {
                throw new System.ArgumentNullException(nameof(key));
            }

            if (!Dictionary.TryGetValue(domain, out var domainDictionary))
            {
                throw new System.Collections.Generic.KeyNotFoundException($@"""{domain}"" not found.");
            }

            if (!domainDictionary.TryGetValue(key, out var value))
            {
                throw new System.Collections.Generic.KeyNotFoundException($@"""{domain}.{key}"" not found.");
            }

            return value;
        }

        /// <summary>
        /// 删除一个领域的全部值。
        /// </summary>
        /// <param name="domain">领域。</param>
        /// <exception cref="System.ArgumentNullException"><paramref name="domain"/> 为 <see langword="null"/> .</exception>
        /// <exception cref="System.Collections.Generic.KeyNotFoundException">未找到 <paramref name="domain"/> .</exception>
        public static void Remove(object domain)
        {
            if (domain == null)
            {
                throw new System.ArgumentNullException(nameof(domain));
            }

            if (!Dictionary.Remove(domain))
            {
                throw new System.Collections.Generic.KeyNotFoundException($@"""{domain}"" not found.");
            }
        }

        /// <summary>
        /// 删除值。
        /// </summary>
        /// <param name="domain">领域。</param>
        /// <param name="key">键。</param>
        /// <exception cref="System.ArgumentNullException"><paramref name="domain"/> 或 <paramref name="key"/> 为 <see langword="null"/> .</exception>
        /// <exception cref="System.Collections.Generic.KeyNotFoundException">未找到 <paramref name="domain"/> 或未找到 <paramref name="key"/> .</exception>
        public static void Remove(object domain, object key)
        {
            if (domain == null)
            {
                throw new System.ArgumentNullException(nameof(domain));
            }

            if (key == null)
            {
                throw new System.ArgumentNullException(nameof(key));
            }

            if (!Dictionary.TryGetValue(domain, out var domainDictionary))
            {
                throw new System.Collections.Generic.KeyNotFoundException($@"""{domain}.{key}"" not found.");
            }

            if (!domainDictionary.Remove(key))
            {
                throw new System.Collections.Generic.KeyNotFoundException($@"""{domain}.{key}"" not found.");
            }
        }

        /// <summary>
        /// 存在某个领域吗？
        /// </summary>
        /// <param name="domain">领域。</param>
        /// <returns>存在某个领域吗？</returns>
        /// <exception cref="System.ArgumentNullException"><paramref name="domain"/> 为 <see langword="null"/> .</exception>
        public static bool Contains(object domain)
        {
            if (domain == null)
            {
                throw new System.ArgumentNullException(nameof(domain));
            }

            return Dictionary.ContainsKey(domain);
        }

        /// <summary>
        /// 存在某个键吗？
        /// </summary>
        /// <param name="domain">领域。</param>
        /// <param name="key">键。</param>
        /// <returns>存在某个键吗？</returns>
        /// <exception cref="System.ArgumentNullException"><paramref name="domain"/> 或 <paramref name="key"/> 为 <see langword="null"/> .</exception>
        public static bool Contains(object domain, object key)
        {
            if (domain == null)
            {
                throw new System.ArgumentNullException(nameof(domain));
            }

            if (key == null)
            {
                throw new System.ArgumentNullException(nameof(key));
            }

            return Dictionary.TryGetValue(domain, out var domainDictionary) && domainDictionary.ContainsKey(key);
        }
    }
}
